<?php


namespace App\Api\Requests;

use App\Api\Traits\RestfulResponse;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\Response;

class Request extends FormRequest
{
    use RestfulResponse;

    public function authorize()
    {
        // Using policy for Authorization
        return true;
    }


    /**
     * Handle a failed validation attempt.
     *
     * @param  \Illuminate\Contracts\Validation\Validator  $validator
     * @return void
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function failedValidation(Validator $validator)
    {
        $messageMap = $validator->errors()->toArray();
        $firstMessage = $messageMap[array_key_first($messageMap)][0];

        throw new HttpResponseException(
            $this->error($firstMessage)
        );
    }
}
